﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessHandler;
using EasyCode.Utility;
using System.Security.Cryptography;
using System.Text;
public partial class Page_UserListManagement : System.Web.UI.Page
{
    private UserController userController;
    int currentPage = 1;
    int pageSize = 20;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.LoadUserList();
        }
    }
    private void LoadUserList()
    {
        UserController.Instance = null;
        userController = UserController.Instance;
        User condition = new User();
        if (this.DropDownList_Status.SelectedValue != string.Empty)
        {
            condition.UserStatus = this.DropDownList_Status.SelectedValue == "1" ? true : false;
        }
        if (this.DropDownList_Type.SelectedValue != string.Empty)
        {
            condition.UserType = Protector.Int(this.DropDownList_Type.SelectedValue);
        }
        if (!string.IsNullOrEmpty(this.TextBox_UserName.Text))
        {
            condition.UserName = this.TextBox_UserName.Text.Trim();
        }
        int skip = (currentPage - 1) * pageSize;
        int total = 0;
        var list = (from item in userController.Users
                    where (condition.UserName == null || item.UserName.Contains(condition.UserName)) &&
                          (condition.UserStatus == null || item.UserStatus == condition.UserStatus) &&
                          (condition.UserType == null || item.UserType == condition.UserType)
                    orderby item.UserID descending
                    select item);
        total = list.Count();
        List<User> userList = list.Skip(skip).Take(pageSize).ToList();
        if (userList != null && total > 0)
        {
            tr_not_found.Visible = false;
        }
        else
        {
            tr_not_found.Visible = true;
        }
        Repeater_Content.DataSource = userList;
        Repeater_Content.DataBind();

        this.BuildPageList(total);

    }
    public string GetType(object type)
    {
        if (type.ToString() == "1")
        {
            return "User";
        }
        return "Admin";
    }
    protected void Button_Search_Click(object sender, EventArgs e)
    {
        this.LoadUserList();
    }
    protected void UpdatePanelContent_Load(object sender, EventArgs e)
    {
        string arg = Request.Form["__EVENTARGUMENT"];
        if (string.IsNullOrEmpty(arg)) return;
        if (arg != null)
        {
            string[] temp = arg.Split('|');
            if (temp.Length == 2)
            {
                if (temp[1].ToString().ToUpper() == "PAGE")
                {
                    this.currentPage = Protector.Int(temp[0].ToString(), 0);
                    this.LoadUserList();
                }
            }
        }
    }
    protected void Repeater_Content_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int id = Protector.Int(e.CommandArgument, 0);
        UserController.Instance = null;
        userController = UserController.Instance;
        var c = (from item in userController.Users where item.UserID == id select item).SingleOrDefault();
        if (c != null)
        {
            if (!CheckRoles.CanEdit())
            {
                Response.Redirect(ResolveUrl("~/Page/AccessDeny.aspx"));
            }
            if (e.CommandName == "LockUnLockCommand")
            {

                c.UserStatus = !c.UserStatus;
                try
                {
                    userController.SubmitChanges();
                    Message_Alert.SetSuccess(Resources.SystemAccount.UserChangeStatusSuccess);
                    this.LoadUserList();
                }
                catch (Exception ex)
                {
                    Message_Alert.SetError(Resources.SystemAccount.UserChangeStatusFail + " Error: " + ex.Message);
                }
            }
            if (e.CommandName == "ChangeType")
            {
                if (c.UserType == 1)
                {
                    c.UserType = 2;
                }
                else
                {
                    c.UserType = 1;
                }

                try
                {
                    userController.SubmitChanges();
                    Message_Alert.SetSuccess(Resources.SystemAccount.ChangeUserTypeSuccess);
                    this.LoadUserList();
                }
                catch (Exception ex)
                {
                    Message_Alert.SetError(Resources.SystemAccount.ChangeUserTypeFail + " Error: " + ex.Message);
                }
            }

        }
    }
    private void BuildPageList(int numOfRecord)
    {
        string pagingText = string.Empty;
        int numOfPage = (numOfRecord / pageSize);
        if ((numOfRecord % pageSize) > 0)
        {
            numOfPage = numOfPage + 1;
        }
        if (numOfPage > 1)
        {
            string pagingLink = Commons.LinkPageForAdmin(numOfPage, this.currentPage, 11, 3);
            pagingText = "<div class='paginator2 right'>" + pagingLink + "</div>";
        }
        Literal_PagingListBot.Text = Literal_PagingListTop.Text = pagingText;
    }
    protected void Button_Cancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("UserListManagement.aspx");
    }
    protected void Button_Save_Click(object sender, EventArgs e)
    {
        if (!CheckRoles.CanInsert())
        {
            Response.Redirect(ResolveUrl("~/Page/AccessDeny.aspx"));
        }
        if (this.DropDownList_TypeAdd.SelectedIndex < 1)
        {
            this.Message_Alert.SetError(Resources.SystemAccount.ConfirmSelectUserType);
            return;
        }
        userController = UserController.Instance;
        User user = new User();
        user.UserName = this.TextBox_UserNameAdd.Text;
        user.UserPassword = Commons.MD5(this.TextBox_Password.Text.Trim());
        user.UserType = Protector.Int(this.DropDownList_TypeAdd.SelectedValue);
        try
        {
            int check = (from o in userController.Users where o.UserName == this.TextBox_UserNameAdd.Text.Trim() select o).Count();
            if (check > 0)
            {
                this.Message_Alert.SetError(Resources.SystemAccount.UserAlreadyExisted);
                return;
            }
            user.UserStatus = true;
            user.UserDesceription = this.TextBox_Description.Text.Trim();
            userController.Users.InsertOnSubmit(user);
            userController.SubmitChanges();
            this.Message_Alert.SetSuccess(string.Format(Resources.SystemAccount.SeveSuccess, user.UserName));
            this.TextBox_UserNameAdd.Text = string.Empty;
            this.TextBox_Password.Text = string.Empty;
            this.LoadUserList();
            this.TextBox_UserName.Text = string.Empty;
            this.DropDownList_TypeAdd.SelectedIndex = 0;
        }
        catch (Exception ex)
        {
            this.Message_Alert.SetError(string.Format(Resources.SystemAccount.SaveFail, user.UserName, ex.Message));
        }
    }
}